package com.sun.newapp;

import android.content.Context;
import android.graphics.Rect;
import android.util.DisplayMetrics;
import android.util.TypedValue;
import android.view.View;
import android.widget.Toast;

import java.util.regex.Matcher;
import java.util.regex.Pattern;


/**
 * Created by 吸氧羊 on 2016/4/1.
 */
public class MyUtils {


    public static Toast mToast;

    public static void showToast(Context mContext, String msg) {
        if (mToast == null) {
            mToast = Toast.makeText(mContext, "", Toast.LENGTH_SHORT);
        }
        mToast.setText(msg);
        mToast.show();
    }

    /**
     * dip 转换成 px
     *
     * @param dip
     * @param context
     * @return
     */
    public static float dip2Dimension(float dip, Context context) {
        DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics();
        return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dip, displayMetrics);
    }

    /**
     * @param dip
     * @param context
     * @param complexUnit {@link TypedValue#COMPLEX_UNIT_DIP} {@link TypedValue#COMPLEX_UNIT_SP}}
     * @return
     */
    public static float toDimension(float dip, Context context, int complexUnit) {
        DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics();
        return TypedValue.applyDimension(complexUnit, dip, displayMetrics);
    }

    /**
     * 获取状态栏高度
     *
     * @param v
     * @return
     */
    public static int getStatusBarHeight(View v) {
        if (v == null) {
            return 0;
        }
        Rect frame = new Rect();
        v.getWindowVisibleDisplayFrame(frame);
        return frame.top;
    }



    /**
     * 取得当前时间戳（精确到秒）
     *
     * @return
     */
    public static int timeStamp() {
        long time = System.currentTimeMillis();
        return (int) (time/1000);
    }
    public static boolean isMobileNumber(String phone) {
        Pattern p = Pattern.compile("^((13[0-9])|(14[5,7])|(17[0-9])|(19[0-9])|(15[^4])|(18[0-9])|(16[0-9])|(19[0-9])|(20[0-2]))+\\d{8}$");
//        Pattern p = Pattern.compile("^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1})|20[0|1|2])+\\d{8})$");
        Matcher m = p.matcher(phone);
        return m.matches();
    }
    public static boolean isEmail(String email) {
        Pattern p = Pattern.compile("^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+((\\.[a-zA-Z0-9_-]{2,3}){1,2})$");
        Matcher m = p.matcher(email);
        return m.matches();
    }
    public static boolean isIDCard(String idcard) {
        Pattern p = Pattern.compile("^(^[1-9]\\d{7}((0\\d)|(1[0-2]))(([0|1|2]\\d)|3[0-1])\\d{3}$)|(^[1-9]\\d{5}[1-9]\\d{3}((0\\d)|(1[0-2]))(([0|1|2]\\d)|3[0-1])((\\d{4})|\\d{3}[Xx])$)$");
        Matcher m = p.matcher(idcard);
        return m.matches();
    }
    public static boolean isNumeric(String str){
        Pattern pattern = Pattern.compile("[0-9]*");
        Matcher isNum = pattern.matcher(str);
        if( !isNum.matches() ){
            return false;
        }
        return true;
    }

    /**
     * 秒转化为天时分秒
     * @param seconds
     */
    public static String formattime(int seconds) {
        int day=0,hours=0,minutes=0;
        day = seconds/(60*60*24);
        seconds -= day*60*60*24;
        hours = seconds/(60*60);
        seconds -= hours*60*60;
        minutes= seconds/60;
        seconds -= minutes*60;
        return day+"天"+hours+"小时"+minutes+"分"+seconds+"秒";
//        System.out.println("总共"+day+"天"+hours+"小时"+minutes+"分"+seconds+"秒");
    }
}
